package org.example.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.example.entity.RoleMenu;
import org.example.entity.TbRole;
import org.example.service.IRoleMenuService;
import org.example.service.ITbRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhaowenting
 * @since 2024-03-22
 */
@RestController
@RequestMapping("/tb-role")
public class TbRoleController {
    @Autowired
    ITbRoleService iTbRoleService;
    @Autowired
    IRoleMenuService iRoleMenuService;

    /**
     * 角色列表
     * @return
     */
    @RequestMapping("roleList")
    public List<TbRole> roleList(){
        final List<TbRole> list = iTbRoleService.listAll();
        return list;
    }

    /**
     * 角色添加
     * @param tbRole
     * @return
     */
    @RequestMapping("addRole")
    public String addRole(@RequestBody Map tbRole){
        System.out.println("从前端传过来的角色数据========"+tbRole);
        //将map类型转换为TbRole类型
        TbRole newRole = BeanUtil.copyProperties(tbRole, TbRole.class);
        System.out.println("类型转换后的角色======"+newRole);
        /**
         * new一个hashset去重 获取角色的菜单id
         */

        String menuIds = (String) tbRole.get("menuIds");
        System.out.println("前端传过来的菜单id+"+menuIds);
        /**
         * 将字符串分割成数组
         */
        String[] split = menuIds.split(",");
        /**
         * 将split  放到set集合 去重
         */
        Set<String> integers = new HashSet<>(Arrays.asList(split));
        System.out.println("分割后的菜单id->menuId为："+split);
        String[] array = integers.toArray(new String[0]);
        //判断角色id是都为空
        //如果不为空 去查询这个角色在roleMenu表关联的数据
        //然后根据角色id删掉这些数据
        if(newRole.getId()!=null){
            LambdaQueryWrapper<RoleMenu> tbRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();
            tbRoleLambdaQueryWrapper.eq(RoleMenu::getRoleId, newRole.getId());
            List<RoleMenu> list = iRoleMenuService.list(tbRoleLambdaQueryWrapper);
            for (RoleMenu roleMenu : list) {
                iRoleMenuService.removeById(roleMenu);
            }
        }
        //往角色表里添加/修改角色
        newRole.setCreateTime(new Date());
        iTbRoleService.saveOrUpdate(newRole);
        System.out.println("角色添加成功:===="+newRole);
        /**
         * 往角色菜单表里添加角色菜单
         */
        RoleMenu roleMenu = new RoleMenu();
        for (String menuId : array) {
            System.out.println("去重后的菜单id为："+menuId);
            roleMenu.setRoleId(newRole.getId());
            roleMenu.setMenuId(Integer.parseInt(menuId));
            iRoleMenuService.save(roleMenu);
        }

        System.out.println("id长度===="+integers.size());
        return "success";
    }

    /**
     * 角色的软删除
     * 根据前端传过来的主键id修改del_id
     * @param id
     * @return
     */
    @RequestMapping("delRoleById")
    public String delRoleById(Integer id){
        TbRole tbRole = iTbRoleService.getById(id);
        tbRole.setDelId(2);
        iTbRoleService.updateById(tbRole);
        return "success";
    }

    /**
     * 根据角色的id去查询关联的菜单id
     * @param roleId
     * @return
     */
    @RequestMapping("menuListByRoleId")
    public List<RoleMenu> menuListByRoleId(Integer roleId){
        LambdaQueryWrapper<RoleMenu> roleMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roleMenuLambdaQueryWrapper.eq(RoleMenu::getRoleId,roleId);
        List<RoleMenu> list = iRoleMenuService.list(roleMenuLambdaQueryWrapper);
        return list;
    }

}
